package com.shujia.sql

import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.{Column, DataFrame, SparkSession}
import org.apache.spark.storage.StorageLevel

object Demo7Burk {
  def main(args: Array[String]): Unit = {

    val spark: SparkSession = SparkSession
      .builder()
      .master("local")
      .appName("student")
      .config("spark.sql.shuffle.partitions", 1)
      .getOrCreate()

    import spark.implicits._
    import org.apache.spark.sql.functions._

    //1、读取数据
    val burk: DataFrame = spark.read
      .format("csv")
      .option("sep", ",")
      .schema("burk STRING,year STRING,tsl01 DOUBLE,tsl02 DOUBLE,tsl03 DOUBLE,tsl04 DOUBLE,tsl05 DOUBLE,tsl06 DOUBLE,tsl07 DOUBLE,tsl08 DOUBLE,tsl09 DOUBLE,tsl10 DOUBLE,tsl11 DOUBLE,tsl12 DOUBLE")
      .load("data/burk.txt")

    //对多次使用的DF 进行缓存
    burk.persist(StorageLevel.MEMORY_ONLY_SER)


    /**
      * array
      *
      */

    spark.sql(
      """
        |select explode(array(1,2,3,4,5,6,7,8))
        |
      """.stripMargin).show()


    /**
      * map
      */

    spark.sql(
      """
        |
        |select explode(map('001','张三','002','李四','003','王五'))
        |
      """.stripMargin).show()

    /**
      * sql
      *
      */
    burk.createOrReplaceTempView("burk")

    /**
      * sum over 有两种情况
      * 1、只分区不排序，全局求和
      * 2，分区排序，累加求和
      *
      */

    /**
      *
      * 1、统计每个公司每年按月累计收入  行转列 --> sum窗口函数
      * 输出结果
      * 公司代码,年度,月份,当月收入,累计收入
      *
      */
    spark.sql(
      """
        |
        |select
        |burk,year,month,pic,sum(pic) over(partition by burk,year order by month) as sumPic
        |from (
        |select
        |burk,year,month,pic
        |from
        |burk lateral view explode(map(1,tsl01,2,tsl02,3,tsl03,4,tsl04,5,tsl05,6,tsl06,7,tsl07,8,tsl08,9,tsl09,10,tsl10,11,tsl11,12,tsl12)) t1 as month,pic
        |) as a
        |
      """.stripMargin)
    //.show()

    /**
      * DSL
      *
      */

    val m: Column = map(
      expr("1"), $"tsl01",
      expr("2"), $"tsl02",
      expr("3"), $"tsl03",
      expr("4"), $"tsl04",
      expr("5"), $"tsl05",
      expr("6"), $"tsl06",
      expr("7"), $"tsl07",
      expr("8"), $"tsl08",
      expr("9"), $"tsl09",
      expr("10"), $"tsl10",
      expr("11"), $"tsl11",
      expr("12"), $"tsl12"
    )

    burk
      //将多列转换多行
      .select($"burk", $"year", explode(m) as Array("month", "pic"))
      //计算累计金额
      .withColumn("sumPic", sum($"pic") over Window.partitionBy($"burk", $"year").orderBy($"month"))
    //.show(100)


    /**
      * 2、统计每个公司当月比上年同期增长率  行转列 --> lag窗口函数
      * 公司代码,年度,月度,增长率（当月收入/上年当月收入 - 1）
      */
    burk
      //将多列转换多行
      .select($"burk", $"year", explode(m) as Array("month", "pic"))
      //湖区上年同期的收入金额
      .withColumn("lastPic", lag($"pic", 1, 0) over Window.partitionBy($"burk", $"month").orderBy($"year"))
      //计算增长率
      .withColumn("p", $"pic" / $"lastPic" - 1)
      //整理数据
      .select($"burk", $"year", $"month", when($"p".isNull, 1.0).otherwise(round($"p", 5)) as "p")

      .show(1000)
  }

}
